#include <avr/io.h>
#include "avr_compiler.h"
#include "clksys_driver.h"

#include "pc_com.h"
#include "IRcom.h"
#include "varpot.h"
#include "i2c.h"

int main (void)
{

	// 32MHz clock
	CLKSYS_Enable( OSC_RC32MEN_bm );
	CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc );
	do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 );
	CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc );

	pc_com_init();
	ir_com_init();
	i2c_init();
	varpot_init();

	// Startup LED
	PORTE.DIRSET = PIN1_bm;
	PORTB.DIRSET = PIN2_bm;
	PORTB.DIRSET = PIN3_bm;
	
	// Everything's okay Blinkenlight
    TCE0.CTRLB = TC_WGMODE_SS_gc; // Single Slope PWM as Waveform Generation mode. 
    TC_SetPeriod(&TCE0, 12000);
    TC_SetCompareB(&TCE0, 1000);
    TC0_ConfigClockSource(&TCE0, TC_CLKSEL_DIV1024_gc);
    TCE0.CTRLB |= TC0_CCBEN_bm;   // Enable the CC channel

	_delay_ms(10000);
	_delay_ms(10000);

	// Pulse Things
/*	TCE0.CTRLB = TC_WGMODE_SS_gc;
    TC_SetPeriod(&TCE0, 10000);
    TC_SetCompareB(&TCE0, 5000);
    TC0_ConfigClockSource(&TCE0, TC_CLKSEL_DIV1024_gc);
    TCE0.CTRLB |= TC0_CCBEN_bm;   // Enable the CC channel
	TCE0.INTCTRLA = ( TCE0.INTCTRLA & ~TC0_OVFINTLVL_gm ) | TC_OVFINTLVL_LO_gc; // Enable overflow interrupt
	TCE0.INTCTRLB = ( TCE0.INTCTRLB & ~TC0_CCBINTLVL_gm ) | TC_CCBINTLVL_LO_gc; // Enable compare interrupt
*/
	PMIC.CTRL |= PMIC_LOLVLEX_bm;

	
	while (true)
	{
		//printf("ERIK");
//		nop();
	}
}
